package com.ourcauser.worn.beans;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.alibaba.fastjson.JSON;
import com.ourcauser.worn.entities.FuncTrace;
import com.ourcauser.worn.services.IFuncTraceService;
import com.ourcauser.worn.utils.HttpRequest;
import com.ourcauser.worn.vo.AmapLocat;

public class QueueMessageListen implements MessageListener {
	
	@Autowired
	@Qualifier("funcTraceService")
	private IFuncTraceService funcTraceService;
	
    public void onMessage(Message message) {        
        try {
        	
        	ObjectMessage msg = (ObjectMessage)(message);
        	
        	if(msg!=null) {
        		
        		FuncTrace voteFuncTrace = (FuncTrace)msg.getObject();
        		
        		// 1.1 IP 转地理位置
        		String IP = voteFuncTrace.getIP().toString();
                String sr=HttpRequest.sendPost("https://restapi.amap.com/v4/ip", "key= <个人申请的 PKEY> &ip=" + IP);
                
                AmapLocat local=JSON.parseObject(sr,AmapLocat.class);
                if(local!=null) {
                	if(local.getErrcode()==0) {
                		String lat = String.valueOf(local.getData().getLat());
                		String lng = String.valueOf(local.getData().getLng());
                		
                		voteFuncTrace.setLatitude(lat);
                		voteFuncTrace.setLongitude(lng);
                	}
                }                
        		
                // 1.2 保存至数据库
        		funcTraceService.insert(voteFuncTrace);
        	}
        	
        } catch (Exception e) {
            e.printStackTrace();            
        }
    }
}
